package com.brian.app_core_libstarter.application.impl;

import com.brian.app_core_libstarter.application.task.ThreadTask;

/**
 * @ProjectName: PathFinder
 * @Package: com.brian.app_core_libstarter.application.impl
 * @ClassName: DispatchRunnable
 * @Description: 任务的真实 执行类
 * @Author: Brian
 * @CreateDate: 2019/11/23 21:53
 * @UpdateUser: Brian
 * @UpdateDate: 2019/11/23 21:53
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DispatchRunnable implements Runnable {

    private ThreadTask threadTask;

    private FinishCallBack finishCallBack;

    private StartCallBack startCallBack;

    public DispatchRunnable task(ThreadTask threadTask) {
        this.threadTask = threadTask;
        return this;
    }

    public DispatchRunnable finishCallBack(FinishCallBack finishCallBack) {
        this.finishCallBack = finishCallBack;
        return this;
    }

    public DispatchRunnable startCallBack(StartCallBack startCallBack) {
        this.startCallBack = startCallBack;
        return this;
    }

    @Override
    public void run() {
        if(null != threadTask){
            if(null != startCallBack){
                startCallBack.onStart(threadTask);
            }
            threadTask.run();
            if(null != finishCallBack){
                finishCallBack.onFinish(threadTask);
            }
        }
    }

    public static DispatchRunnable builder(){
        return new DispatchRunnable();
    }

    public interface FinishCallBack {
        void onFinish(ThreadTask threadTask);
    }

    public interface StartCallBack {
        void onStart(ThreadTask threadTask);
    }
}
